package analyticsServer;

import java.rmi.RemoteException;
import java.util.Calendar;

import analyticsServer.dataLayer.DataLayer;


import common.*;

public class Statistics {

	public Statistics() {
		
	}
	
	public static StatisticsEvent calculateAverageAuctionTime(){
		
		double average=DataLayer.getTotalAuctionDurations()/DataLayer.getTotalAuctionCount();
		
		try {
			return new StatisticsEvent("AUCTION_TIME_AVG", average);
		} catch (RemoteException e) {
			
			e.printStackTrace();
			return null;
		}
	}
	
	public static StatisticsEvent calculateMaxBidPrice(){
		double maxPrice=DataLayer.getMaxBidPrice();
		try {
			return new StatisticsEvent("BID_PRICE_MAX", maxPrice);
		} catch (RemoteException e) {
			
			e.printStackTrace();
			return null;
		}
	}
	
	public static StatisticsEvent calculateBidCountPerMinute(){
		double count=DataLayer.getBidCount();
		long firstTimeStamp=DataLayer.getFirstBidTime();
		
		try {
			return new StatisticsEvent("BID_COUNT_PER_MINUTE", count/(double)(Calendar.getInstance().getTimeInMillis()-firstTimeStamp)/60000);
		} catch (RemoteException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public static StatisticsEvent calculateSuccesfulAuctionRatio(){
		double auction=DataLayer.getFinishedAuctionCount();
		double won=DataLayer.getBidCount();		
		try {
			return new StatisticsEvent("AUCTION_SUCCESS_RATIO", won/auction);
		} catch (RemoteException e) {

			e.printStackTrace();
			return null;
		}
	}
	
	public static StatisticsEvent calculateUserSessionTimeMax(){
	
		try {
			return new StatisticsEvent("USER_SESSIONTIME_MAX", DataLayer.getMaxSessionDuration());
		} catch (RemoteException e) {
			e.printStackTrace();
			return null;
			}
	}
	public static StatisticsEvent calculateUserSessionTimeMin(){
	
		try {
			return new StatisticsEvent("USER_SESSIONTIME_MIN",DataLayer.getMinSessionDuration());
		} catch (RemoteException e) {
			e.printStackTrace();
			return null;
		}
	}
	public static StatisticsEvent calculateUserSessionTimeAverage(){
		double average=DataLayer.getTotalSessionDurations()/DataLayer.getTotalSessionCount();
		
		try {
			return new StatisticsEvent("USER_SESSIONTIME_AVG", average);
		} catch (RemoteException e) {
			e.printStackTrace();
			return null;
		}
	}
}
